
$(document).ready(function(){
    // render
    loadData();
    // event
    bindEvent();
});

function loadDealCode(){
    var now = new Date();
    ProductDetail_code_auto = "KM" + now.getTime();
    ISS.DOM.setVal('#DealCommon_code', ProductDetail_code_auto);
}

function loadDateCreate(){
    var now = new Date();
    var dateCreate = now.getTime();

    ISS.DOM.setVal('#DealCommon_dateCreate', ISS.DATE.getFullDate(new Date(dateCreate)));
    ISS.DOM.setVal('#DealCommon_dateBegin', ISS.DATE.getFullDate(new Date(dateCreate)));
    ISS.DOM.setVal('#DealCommon_dateEnd', ISS.DATE.getFullDate(new Date(dateCreate)));
}

function loadData() {

    loadDealCode();
    loadDateCreate();


    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/ad_GetAllDeal',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapListDealToView(responseContent);
            mapListDealToViewDetail(responseContent);
            mapDealToViewCommon(responseContent);
        }
    });

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/ad_GetAllDealDetail',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapDealDetailToViewCommon(responseContent);
        }
    });

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/getAllMemberShowInDealCommon',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapAllMemberToView(responseContent);
        }
    });

/*    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/getAllProductDetailShowInDetail',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapProductDetailCodeToView(responseContent);
        }
    });*/


    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/ad_GetAllColor',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapAllColorToView(responseContent);
        }
    });

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/ad_GetAllSize',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapAllSizeToView(responseContent);
        }
    });

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/ad_GetAllMotifInProductDetail',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapAllMotifToView(responseContent);
        }
    });

    ISS.BACKEND.request({
        url: '../../service/admin/getAllProductListInProduct',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapAllProductCategoryToViewInProduct(responseContent);
        }
    });
}

function bindEvent() {
    ISS.EVENT.bindEvent('#DealCommon_Add1', 'click', function () {
        if(validate()){
            sendInfo();
        }
        else {
            alert('Nhap lai');
            var htmlText = '<div class="box s-error mbs msgBox l-pageWrapper mbm">\
                                <div class="pam">Tạo khuyến mãi mới thất bại</div>\
                            </div>';
            ISS.DOM.before('.l-register__benefits', htmlText);
        }
    });

    ISS.EVENT.bindEvent('#DealCommon_Modified', 'click', function () {
        toEditPage();
    });

    ISS.EVENT.bindEvent('#DealDetailCommon_Modified', 'click', function () {
        toEditDetailPage();
    });

    ISS.EVENT.bindEvent('#DealCommon_Retype1', 'click', function () {
        loadDealCode();
        loadDateCreate();
        ISS.DOM.setVal('#DealCommon_name', "");
        ISS.DOM.setVal('#DealCommon_user_option', "default_value");
    });

/*    ISS.EVENT.bindEvent('#ProductForDeal_Search', 'click', function () {
        var color = ISS.DOM.getVal('#ProductForDeal_color');
        var motif = ISS.DOM.getVal('#ProductForDeal_motif');
        var size = ISS.DOM.getVal('#ProductForDeal_size');

        ISS.BACKEND.request({
            url: ISS.SESSION.getSession('contextPath') + '/service/admin/getDataDetailForDeal/' + color + '/' + motif + '/' + size,
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            type: 'GET',
            dataType: 'json',
            success: function (data) {
                var responseContent = data.responseObject;
                mapAllProductDetailSearchToView(responseContent);
            }
        });
    });*/

/*    ISS.EVENT.bindEvent('#ProductForDealCommon_Add', 'click', function () {
//        toEditDetailPage();
        var _listCTSP = [];
        var _maCTSP = [];

        ISS.DOM.forEach('#form-ProductForDealCommon input[type="checkbox"]:checked', function(){
            _maCTSP.push(ISS.DOM.getVal(this));
        });

        if(_maCTSP.length > 0) {
            for(var i = 0; i < _maCTSP.length; i++) {
                var _product = {};
                _product.maCTSP = _maCTSP[i];
                _listCTSP.push(_product);
            }
//            _listBillDelete = _listHoaDon;

//            ISS.CONTROL.lockScreen('#content');
            ISS.BACKEND.request({
                url: SERVER.contextPath + '/service/admin/insertProductDetail/',
                headers: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/json'
                },
                type: 'POST',
                dataType: 'json',
                data: JSON.stringify(_listCTSP),
                success: function (data) {
                    if(data.code == 0) {
//                        toDetailPage();
//                        ISS.DOM.removeClass('#boxTrackDeleteSuccess', 'hideBox');
//                        ISS.CONTROL.unLockScreen();
                    }
                }
            });
        }
        else {
            alert('Bạn chưa chọn chi tiết sản phẩm nào');
        }
    });*/

    ISS.EVENT.bindEvent('#DealCommon_Delete', 'click', function () {
        var _maKM = [];
        ISS.DOM.forEach('#DealCommon_list input[type="checkbox"]:checked', function(){
            _maKM.push($(this).val());
        });

        if(_maKM.length == 1) {
            ISS.BACKEND.request({
                url: ISS.SESSION.getSession('contextPath') + '/service/admin/getDataDetailOfDealCommon/' + _maKM[0],
                headers: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/json'
                },
                type: 'GET',
                dataType: 'json',
                success: function (data) {
                    var responseContent = data.responseObject;
                    if(responseContent){
                        alert('Co chi tiet');
//                        ISS.DOM.removeClass('#boxTrackDeleteConfirmContinue', 'hideBox');
                    } else {
                        alert('Ko co chi tiet');
                        deleteDealCommon(_maKM[0]);
                    }
                }
            });
        }
        else {
            alert('Không được chọn hơn một dòng');
        }
    });

    ISS.EVENT.bindEvent('#DealDetail_Search', 'click', function () {
        var maDMSP = ISS.DOM.getVal('#DealDetail_OptionListProduct');
        var sexual = ISS.DOM.getVal('#DealDetail_sexual');
        if(sexual == 'NAM'){
            ISS.BACKEND.request({
                url: ISS.SESSION.getSession('contextPath') + '/service/admin/getProductWidthCategoryM/' + maDMSP,
                type: 'GET',
                success: function (data) {
                    var responseInfo = data.responseObject;
                    mapAllProductToView(responseInfo);
                }
            });
        }
        else {
            ISS.BACKEND.request({
                url: ISS.SESSION.getSession('contextPath') + '/service/admin/getProductWidthCategoryF/' + maDMSP,
                type: 'GET',
                success: function (data) {
                    var responseInfo = data.responseObject;
                    mapAllProductToView(responseInfo);
                }
            });
        }
    });

    ISS.EVENT.bindEvent('#ProductForDealCommon_Detail_Search', 'click', function () {
        showDetailInPage();
    });

    ISS.EVENT.bindEvent('#ProductDetail_Add', 'click', function () {
        insertProductDealForDeal();
    });
}

function insertProductDealForDeal(){
    var _maKhuyenMai = ISS.DOM.getVal('#DealCommon_code');
    var _percent = ISS.DOM.getVal('#inputPercentForDeal');

    var _listCTSP = [];
    var _maCTSP = [];

    ISS.DOM.forEach('#form-ProductDetailForDealCommon input[type="checkbox"]:checked', function(){
        _maCTSP.push(ISS.DOM.getVal(this));
    });

    if(_maCTSP.length > 0) {
        for(var i = 0; i < _maCTSP.length; i++) {
            var _product = {};
            _product.maCTSP = _maCTSP[i];
            _listCTSP.push(_product);
        }

        ISS.BACKEND.request({
            url: ISS.SESSION.getSession('contextPath') + '/service/admin/insertProductDetailForDeal/' +_maKhuyenMai + '/' + _percent,
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            type: 'POST',
            dataType: 'json',
            data: JSON.stringify(_listCTSP),
            success: function (data) {
                if(data.code == 0) {
                    alert('Thêm thành công');
                }
            }
        });
    }
    else {
        alert('B?n chưa ch?n chi ti?t s?n ph?m nào');
    }
}

function showDetailInPage() {
    var _maSP = [];
    ISS.DOM.forEach('#form-ProductForDealCommon input[type="checkbox"]:checked', function(){
        _maSP.push($(this).val());
    });

    if(_maSP.length == 1) {
//        var _url = '/service/admin/toProductDetailPage/' + _maSP[0];
//        ISS.WINDOW.location(_url);
        ISS.BACKEND.request({
            url: ISS.SESSION.getSession('contextPath') + '/service/admin/getDataDetailOfDataCommon/' + _maSP[0],
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            type: 'GET',
            dataType: 'json',
            success: function (data) {
                var responseContent = data.responseObject;
                mapAllProductDetailSearchToView(responseContent);
            }
        });
    }
    else {
        alert('Không được chọn hơn một dòng');
    }
}

function deleteDealCommon(maKM) {
    var _khuyenMai = {};
    _khuyenMai.maKM = maKM;
    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/deleteDealCommon/',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(_khuyenMai),
        success: function (data) {
            if(data.code == 0) {
                alert('Thanh cong');
            }
        }
    });
}

function sendInfo() {
    var deal = {};
    var member = {};

    deal.maKM = ISS.DOM.getVal('#DealCommon_code');
    member.maTV  = ISS.DOM.getVal('#DealCommon_user_option');
    deal.thongTinThanhVien = member;
    deal.tenKM = ISS.DOM.getVal('#DealCommon_name');

    deal.ngayTaoKM = ISS.DATE.changeFormatDate(ISS.DOM.getVal('#DealCommon_dateCreate'));
    deal.ngayBD = ISS.DATE.changeFormatDate(ISS.DOM.getVal('#DealCommon_dateBegin'));
    deal.ngayKT = ISS.DATE.changeFormatDate(ISS.DOM.getVal('#DealCommon_dateEnd'));

    ISS.BACKEND.request({
        url: '../../service/admin/header/adDealCommon',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(deal),
        success: function (data) {
            var responseContent = data.responseObject;
        }
    });
}

function toEditPage() {
    var _maKM = [];
    ISS.DOM.forEach('#form-DealCommon input[type="checkbox"]:checked', function(){
        _maKM.push($(this).val());
    });

    if(_maKM.length == 1) {
        var _url = '/service/admin/toDealPage/' + _maKM[0];
        ISS.WINDOW.location(_url);
    }
    else {
        alert('Bạn chưa chọn hoặc chọn hơn 1 dòng');
    }
}

function toEditDetailPage() {
    var _maCTKM = [];
    ISS.DOM.forEach('#form-DealDetail input[type="checkbox"]:checked', function(){
        _maCTKM.push($(this).val());
    });

    if(_maCTKM.length == 1) {
        var _url = '/service/admin/toDealDetailPage/' + _maCTKM[0];
        ISS.WINDOW.location(_url);
    }
    else {
        alert('Bạn chưa chọn hoặc chọn hơn 1 dòng');
    }
}

function clearValidate() {
    ISS.DOM.removeClass('#DealCommon_code', 'error');
    ISS.DOM.removeClass('#DealCommon_name', 'error');
}

function validate() {
    var result = true;
    var dateCreate = ISS.DOM.getVal('#DealCommon_dateCreate');
    var dateBegin = ISS.DOM.getVal('#DealCommon_dateBegin');
    var dateEnd = ISS.DOM.getVal('#DealCommon_dateEnd');
    ISS.VALIDATION.compareDate(dateCreate, dateBegin, dateEnd);
/*    if(!) {
        result = false;
    } else {
        result = true;
    }*/

    return result;
}

function mapListDealToView(listdeal) {
    var html = '<option selected="selected" value="default_value">Mời chọn...</option>';
    for(var i=0; i<listdeal.length; i++) {
        html += '<option value="' + listdeal[i].maKM + '">' + listdeal[i].tenKM + '</option>';
    }
    ISS.DOM.html('#DealCommon_option', html);
}

function mapListDealToViewDetail(listdeal) {
    var html = '<option selected="selected" value="default_value">Mời chọn...</option>';
    for(var i=0; i<listdeal.length; i++) {
        html += '<option value="' + listdeal[i].maKM + '">' + listdeal[i].tenKM + '</option>';
    }
    ISS.DOM.html('#DealDetail_Option', html);
}

function mapDealToViewCommon(listdealcommon) {

        var rows = listdealcommon.length;
        var columns = 1;
        var str = '';
        str += '<table id="DealTable" class="tableDealWidth align-center">';
        str += '<thead>';
        str += '   <tr>';
        str += '       <th class="columnDealSTT">STT</th>';
        str += '       <th class="columnDealStick">Tick</th>';
        str += '       <th class="columnDealName">Tên chương trình</th>';
        str += '       <th class="columnDealAuthor">Người tạo</th>';
        str += '       <th class="columnDealDate">Ngày khởi tạo</th>';
        str += '       <th class="columnDealDateBegin">Ngày bắt đầu</th>';
        str += '       <th class="columnDealDateEnd">Ngày kết thúc</th>';
        str += '   </tr>';
        str += '</thead>';
        str += '<tbody>';
        for(var i = 0; i < rows; i++){
            if (i%2==0){
                str += '<tr onmouseover="set_hover(this);" onmouseout="remove_hover(this)" id="showData" class="column-Light tableTransparent">';
            }else{
                str += '<tr onmouseover="set_hover(this);" onmouseout="remove_hover(this)" id="showData" class="column-Light tableTransparent">';
            }

            for (var j = 0; j < columns; j++){
                str += '<td class="columnDealSTT tableDealPadding">' + i + '</td>';
                str += '<td class="columnDealStick tableDealPadding"><input onclick="highlight_row(this)" id="' + listdealcommon[i].maKM + '" type="checkbox" value="' + listdealcommon[i].maKM + '"/></td>';
                str += '<td class="columnDealName tableDealPadding">' + listdealcommon[i].tenKM + '</td>';
                str += '<td class="columnDealAuthor tableDealPadding">' + listdealcommon[i].thongTinThanhVien.maTV + '</td>';
                str += '<td class="columnDealDate tableDealPadding">' + ISS.DATE.getFullDate(new Date(listdealcommon[i].ngayTaoKM)) + '</td>';
                str += '<td class="columnDealDateBegin tableDealPadding">' + ISS.DATE.getFullDate(new Date(listdealcommon[i].ngayBD)) + '</td>';
                str += '<td class="columnDealDateEnd tableDealPadding">' + ISS.DATE.getFullDate(new Date(listdealcommon[i].ngayKT)) + '</td>';
            }
            str += '</tr>';
        }
        str +=   '</tbody>';
        str += '</table>';
    ISS.DOM.html('#DealCommon_list', str);
}

function mapDealDetailToViewCommon(detail) {

    var rows = detail.length;
    var columns = 1;
    var str = '';
    str += '<table class="scroll align-center">';
    str += '<thead>';
    str += '   <tr>';
    str += '       <th class="columnSTT">STT</th>';
    str += '       <th class="columnStick">Tick</th>';
    str += '       <th class="columnDealName">Tên KM</th>';
    str += '       <th class="column-Deal-date">Tên SP</th>';
    str += '       <th class="column-Deal-date">Giá gốc</th>';
    str += '       <th class="column-Deal-date-begin">Giảm giá (%)</th>';
    str += '       <th class="column-Deal-date">Giá KM</th>';
    str += '   </tr>';
    str += '</thead>';
    str += '<tbody>';
    for(var i = 0; i < rows; i++){
        if (i%2==0)
        {
            str += '<tr class="column-Dark">';
            var n="column-Dark";
        }
        else
        {
            str += '<tr class="column-Light">';
            n="column-Light";
        }

        for (var j = 0; j < columns; j++){
            str += '<td class="columnSTT">' + i + '</td>';
            str += '<td class="columnStick"><input id=' + detail[i].maCTKM + ' type="checkbox" value="' + detail[i].maCTKM + '"/></td>';
            str += '<td class="column-Deal-author">' + detail[i].khuyenMai.tenKM + '</td>';
            str += '<td class="column-Deal-date">' + detail[i].chiTietSanPham.sanPham.tenSP + ' - ' + detail[i].chiTietSanPham.mauSac.tenMau + ' - ' + detail[i].chiTietSanPham.kichThuoc.tenSize + ' - ' + detail[i].chiTietSanPham.sanPham.doiTuong + '</td>';
            str += '<td class="column-Deal-date">' + detail[i].chiTietSanPham.giaSP + '</td>';
            str += '<td class="column-Deal-date-begin">' + detail[i].phanTram + '</td>';
            str += '<td class="column-Deal-date">' + detail[i].chiTietSanPham.giaSP + '</td>';
        }
        str += '</tr>';
    }
    str +=   '</tbody>';
    str += '</table>';
    ISS.DOM.html('#DealDetailCommon_list', str);
}

function mapAllMemberToView(member) {
//    var code = ISS.DOM.getVal('#DealCommon_user_hidden');
//    var n = '';
//    var html = '<option value="default_value">...</option>';
//    for(var i=0; i<member.length; i++) {
//        if (member[i].maLoai == code){
//            n = 'selected="selected"';
//        }
//        else{
//            n = '';
//        }
//        html += '<option value="' + member[i].maTV + '" ' + n + '  >' + member[i].maTV + ' - ' + member[i].ho + ' ' + member[i].ten + '</option>';
//    }
//    ISS.DOM.html('#DealCommon_user_option', html);
    var userNameAdmin = ISS.SESSION.getSession('userNameAdmin').toUpperCase();
    var n = '';
    var html = '';
    for(var i=0; i<member.length; i++) {
        if (member[i].maTV == userNameAdmin){
            n = 'selected="selected"';
        }
        else{
            n = '';
        }
        html += '<option value="' + member[i].maTV + '"' + n + '>' + member[i].maTV + ' - ' + member[i].ho + ' ' + member[i].ten + '</option>';
    }
    ISS.DOM.html('#DealCommon_user_option', html);
}

function mapProductDetailCodeToView(product) {
    var rows = product.length;
    var columns = 1;
    var str = '';
    str += '<table class="scroll align-center">';
    str += '<thead>';
    str += '   <tr>';
    str += '       <th class="columnSTT">STT</th>';
    str += '       <th class="columnStick">Tick</th>';
    str += '       <th class="columnList">Danh mục</th>';
    str += '       <th class="columnTitle-summary">Tên</th>';
    str += '       <th class="columnColor">Màu</th>';
    str += '       <th class="columnSize">Size</th>';
    str += '       <th class="columnSexual">ĐT</th>';
    str += '       <th class="columnPicture">Hình ảnh</th>';
    str += '       <th class="columnPatern">Hoạt tiết</th>';
    str += '       <th class="columnQuantity-Product-Detail-summary">SL hiện có</th>';
    str += '       <th class="columnQuantity-Product-Detail-summary">SL bán</th>';
    str += '       <th class="columnPrice-summary-product-detail">Đơn giá (VND)</th>';
    str += '       <th class="columnPrice-summary-product-detail">Tổng giá (SL*ĐG)</th>';
    str += '   </tr>';
    str += '</thead>';
    str += '<tbody>';
    for(var i = 0; i < rows; i++){
        if (i%2==0)
        {
            str += '<tr class="column-Dark">'
        }
        else
        {
            str += '<tr class="column-Light">'
        }
        for (var j = 0; j < columns; j++){
            str += '<td class="columnSTT">' + (i+1) + '</td>';
            str += '<td class="columnStick"><input id="' + product[i].maCTSP + '" type="checkbox" value="' + product[i].maCTSP + '"/></td>';
            str += '<td class="columnList" id="' + product[i].sanPham.loaiDanhMucSanPham.maLoaiDMSP + '">' + product[i].sanPham.loaiDanhMucSanPham.tenLoaiDMSP + '</td>';
            str += '<td class="columnTitle-summary">' + product[i].sanPham.tenSP + '</td>';
            str += '<td class="columnColor" id="' + product[i].mauSac.maMau + '">' + product[i].mauSac.tenMau + '</td>';
            str += '<td class="columnSize" id="' + product[i].kichThuoc.maSize + '">' + product[i].kichThuoc.tenSize + '</td>';
            str += '<td class="columnSexual">' + product[i].sanPham.doiTuong + '</td>';
            str += '<td class="columnPicture">Picture ' +  i + '.jpg</td>';
            str += '<td class="columnPatern" id="' + product[i].hoaTiet.maHoaTiet + '">' +  product[i].hoaTiet.tenHoaTiet + '</td>';
            str += '<td class="columnQuantity-Product-Detail-summary">' +   product[i].soLuong + '</td>';
            str += '<td class="columnQuantity-Product-Detail-summary"><input id="BillDetail_QuantitySell_' + product[i].maCTSP + '" type="text"/></td>';
            str += '<td class="columnPrice-summary-product-detail">'+ product[i].giaSP +'</td>';
            str += '<td class="columnPrice-summary-product-detail"><input id="BillDetail_TotalPrice_' + product[i].maCTSP + '" type="text"/></td>';
        }
        str += '</tr>';
    }
    str +=   '</tbody>';
    str += '</table>';
    ISS.DOM.html('#DealDetail_product', str);
}

function mapAllProductDetailSearchToView(product) {
    var rows = product.chiTietSanPhams.length;
    var columns = 1;
    var str = '';
    str += '<table id="ProductDetailTable" class="tableProductDetailWidth align-center">';
    str += '<thead>';
    str += '   <tr>';
    str += '       <th class="columnProductDetailSTT">STT</th>';
    str += '       <th class="columnProductDetailStick">Tick</th>';
    str += '       <th class="columnProductDetail_ProductCode">Mã SP</th>';
    str += '       <th class="columnProductDetailCode">Mã CTSP</th>';
    str += '       <th class="columnProductDetail_Category">Danh mục</th>';
    str += '       <th class="columnProductDetailTitle">Tên</th>';
    str += '       <th class="columnProductDetailColor">Màu</th>';
    str += '       <th class="columnProductDetailSize">Size</th>';
    str += '       <th class="columnProductDetailSexual">ĐT</th>';
    str += '       <th class="columnProductDetailPicture">Hình ảnh</th>';
    str += '       <th class="columnProductDetailPatern">Hoạ tiết</th>';
    str += '       <th class="columnProductDetailQuantity">SL</th>';
    str += '       <th class="columnProductDetailPrice">Đơn giá (VND)</th>';
    str += '   </tr>';
    str += '</thead>';
    str += '<tbody>';
    if(product.chiTietSanPhams) {
        for(var i = 0; i < rows; i++){
            if (i%2==0){
                str += '<tr onmouseover="set_hover(this);" onmouseout="remove_hover(this)" id="showData" class="column-Light tableTransparent">';
            }else{
                str += '<tr onmouseover="set_hover(this);" onmouseout="remove_hover(this)" id="showData" class="column-Light tableTransparent">';
            }

            for(var j = 0; j<columns; j++) {
                var _images = product.chiTietSanPhams[i].hinhAnhSanPham;
                var _color = product.chiTietSanPhams[i].mauSac;
                var _size = product.chiTietSanPhams[i].kichThuoc;
                var _motif = product.chiTietSanPhams[i].hoaTiet;
                var _catelogy = product.loaiDanhMucSanPham;
                var _sexual = product.doiTuong;
                var _productname = product.tenSP;
                var _quality = product.chiTietSanPhams[i].soLuong;
                var _price = product.chiTietSanPhams[i].giaSP;

                str += '<td class="columnProductDetailSTT tableProductDetailPadding">' + (i+1) + '</td>';
                str += '<td class="columnProductDetailStick tableProductDetailPadding"><input onclick="highlight_row(this)" id="' + product.chiTietSanPhams[i].maCTSP + '" type="checkbox" value="' + product.chiTietSanPhams[i].maCTSP + '"/></td>';
                str += '<td class="columnProductDetail_ProductCode tableProductDetailPadding word_break" title="' + product.maSP + '">' + product.maSP + '</td>';
                str += '<td class="columnProductDetailCode tableProductDetailPadding word_break" title="' + product.chiTietSanPhams[i].maCTSP + '">' + product.chiTietSanPhams[i].maCTSP + '</td>';
                if(_catelogy) {
                    str += '<td class="columnProductDetail_Category tableProductDetailPadding" id="' + product.loaiDanhMucSanPham.maLoaiDMSP + '">' + product.loaiDanhMucSanPham.tenLoaiDMSP + '</td>';
                } else {
                    str += '<td class="columnProductDetail_Category tableProductDetailPadding">Trống</td>';
                }
                if(_productname) {
                    str += '<td class="columnProductDetailTitle tableProductDetailPadding" title="' + product.tenSP + '">' + product.tenSP.trunc(100,true) + '</td>';
                } else {
                    str += '<td class="columnProductDetailTitle tableProductDetailPadding">Trống</td>';
                }

                if(_color) {
                    str += '<td class="columnProductDetailColor tableProductDetailPadding" id="' + product.chiTietSanPhams[i].mauSac.maMau + '">' + product.chiTietSanPhams[i].mauSac.tenMau + '</td>';
                } else {
                    str += '<td class="columnProductDetailColor tableProductDetailPadding">Trống</td>';
                }

                if(_size) {
                    str += '<td class="columnProductDetailSize tableProductDetailPadding" id="' + product.chiTietSanPhams[i].kichThuoc.maSize + '">' + product.chiTietSanPhams[i].kichThuoc.tenSize + '</td>';
                } else {
                    str += '<td class="columnProductDetailSize tableProductDetailPadding">Trống</td>';
                }

                if(_sexual) {
                    str += '<td class="columnProductDetailSexual tableProductDetailPadding">' + product.doiTuong + '</td>';
                } else {
                    str += '<td class="columnProductDetailSexual tableProductDetailPadding">Trống</td>';
                }

                if(_images[0]) {
                    str += '<td class="columnProductDetailPicture tableProductDetailPadding"><img src="'+ ISS.SESSION.getSession('contextPath') + _images[0].tenHinh + '" width="100px"></td>';
                } else {
                    str += '<td class="columnProductDetailPicture tableProductDetailPadding">Trống</td>';
                }

                if(_motif) {
                    str += '<td class="columnProductDetailPatern tableProductDetailPadding" id="' + product.chiTietSanPhams[i].hoaTiet.maHoaTiet + '">' +  product.chiTietSanPhams[i].hoaTiet.tenHoaTiet + '</td>';
                } else {
                    str += '<td class="columnProductDetailPatern tableProductDetailPadding">Trống</td>';
                }

                if(_quality) {
                    str += '<td class="columnProductDetailQuantity tableProductDetailPadding">' +   product.chiTietSanPhams[i].soLuong + '</td>';
                } else {
                    str += '<td class="columnProductDetailQuantity tableProductDetailPadding">Trống</td>';
                }

                if(_price) {
                    str += '<td class="columnProductDetailPrice tableProductDetailPadding">'+ formatCurency(product.chiTietSanPhams[i].giaSP) +'</td>';
                } else {
                    str += '<td class="columnProductDetailPrice tableProductDetailPadding">Trống</td>';
                }
            }

            str += '</tr>';
        }
    }
    str +=   '</tbody>';
    str += '</table>';

    ISS.DOM.html('#ProductForDealCommon_All', str);

}

function mapAllProductToView(product) {
    var rows = product.length;
    var columns = 1;
    var str = '';

    str += '<table id="ProductTable" class="tableProductForDealWidth align-center">';
    str += '<thead>';
    str += '   <tr>';
    str += '       <th class="columnProductSTT">STT</th>';
    str += '       <th class="columnProductStick">Tick</th>';
    str += '       <th class="columnProductCode">Mã SP</th>';
    str += '       <th class="columnProductList">Danh mục</th>';
    str += '       <th class="columnProductTitle">Tên sản phẩm</th>';
    str += '       <th class="columnProductInfo">Thông tin</th>';
    str += '       <th class="columnProductSexual">ĐT</th>';
//        str += '       <th class="columnProductButtonDetail"></th>';
    str += '   </tr>';
    str += '</thead>';
    str += '<tbody>';
    for(var i = 0; i < rows; i++){
        if (i%2==0){
            str += '<tr onmouseover="set_hover(this);" onmouseout="remove_hover(this)" id="showData" class="column-Light tableTransparent">';
        }else{
            str += '<tr onmouseover="set_hover(this);" onmouseout="remove_hover(this)" id="showData" class="column-Light tableTransparent">';
        }

        for (j = 0; j < columns; j++){
            str += '<td class="columnProductSTT tableProductPadding">' + (i+1) + '</td>';
            str += '<td class="columnStick tableProductPadding"><input onclick="highlight_row(this)" id="' + product[i].maSP + '" type="checkbox" value="' + product[i].maSP + '"/></td>';
            str += '<td class="columnStick tableProductPadding" id="' + product[i].maSP + '">' + product[i].maSP + '</td>';
            str += '<td class="columnProductList tableProductPadding" id="' + product[i].loaiDanhMucSanPham.maLoaiDMSP + '">' + product[i].loaiDanhMucSanPham.tenLoaiDMSP + '</td>';
            str += '<td class="columnProductTitle tableProductPadding">' + product[i].tenSP + '</td>';
            str += '<td class="columnProductInfo tableProductPadding" title="' + product[i].thongTin + '">' + product[i].thongTin.trunc(140,true) + '</td>';
            str += '<td class="columnProductSexual tableProductPadding">' + product[i].doiTuong + '</td>';
//                str += '<td class="columnProductButtonDetail tableProductPadding"><button id="ProductCommon_Search_' + product[i].maSP + '" class="btn btn-dark login__button" type="button">CHI TIẾT</button></td>';
        }
        str += '</tr>';
    }
    str +=   '</tbody>';
    str += '</table>';

    ISS.DOM.html('#ProductForDealCommon_All', str);

}

function mapAllColorToView(color) {
    var html = '';
    for(var i=0; i<color.length; i++) {
        html += '<option value="' + color[i].maMau + '">' + color[i].tenMau + '</option>';
    }

    ISS.DOM.html('#ProductForDeal_color', html);
}

function mapAllSizeToView(size) {
    var html = '';
    for(var i=0; i<size.length; i++) {
        html += '<option value="' + size[i].maSize + '">' + size[i].tenSize + ' - ' + size[i].loaiDanhMuc.tenLoai + '</option>';
    }

    ISS.DOM.html('#ProductForDeal_size', html);
}

function mapAllMotifToView(motif) {
    var html = '';
    for(var i=0; i<motif.length; i++) {
        html += '<option value="' + motif[i].maHoaTiet + '">' + motif[i].tenHoaTiet + '</option>';
    }

    ISS.DOM.html('#ProductForDeal_motif', html);
}

function mapAllProductCategoryToViewInProduct(productcategory) {
    var html = '';
    for(var i=0; i<productcategory.length; i++) {
        html += '<option value="' + productcategory[i].maLoaiDMSP + '">' + productcategory[i].tenLoaiDMSP + '</option>';
    }
    ISS.DOM.html('#DealDetail_OptionListProduct', html);
}

function mapAllProductDetailSearchToView(product) {
    var rows = product.chiTietSanPhams.length;
    var columns = 1;
    var str = '';
    str += '<table id="ProductDetailTable" class="tableProductDetailForDealWidth align-center">';
    str += '<thead>';
    str += '   <tr>';
    str += '       <th class="columnProductDetailSTT">STT</th>';
    str += '       <th class="columnProductDetailStick">Tick</th>';
    str += '       <th class="columnProductDetail_ProductCode">Mã SP</th>';
    str += '       <th class="columnProductDetailCode">Mã CTSP</th>';
    str += '       <th class="columnProductDetail_Category">Danh mục</th>';
    str += '       <th class="columnProductDetailTitle">Tên</th>';
    str += '       <th class="columnProductDetailColor">Màu</th>';
    str += '       <th class="columnProductDetailSize">Size</th>';
    str += '       <th class="columnProductDetailSexual">ĐT</th>';
    str += '       <th class="columnProductDetailPicture">Hình ảnh</th>';
    str += '       <th class="columnProductDetailPatern">Hoạ tiết</th>';
    str += '       <th class="columnProductDetailQuantity">SL</th>';
    str += '       <th class="columnProductDetailPrice">Đơn giá (VND)</th>';
    str += '   </tr>';
    str += '</thead>';
    str += '<tbody>';
    if(product.chiTietSanPhams) {
        for(var i = 0; i < rows; i++){
            if (i%2==0){
                str += '<tr onmouseover="set_hover(this);" onmouseout="remove_hover(this)" id="showData" class="column-Light tableTransparent">';
            }else{
                str += '<tr onmouseover="set_hover(this);" onmouseout="remove_hover(this)" id="showData" class="column-Light tableTransparent">';
            }

            for(var j = 0; j<columns; j++) {
                var _images = product.chiTietSanPhams[i].hinhAnhSanPham;
                var _color = product.chiTietSanPhams[i].mauSac;
                var _size = product.chiTietSanPhams[i].kichThuoc;
                var _motif = product.chiTietSanPhams[i].hoaTiet;
                var _catelogy = product.loaiDanhMucSanPham;
                var _sexual = product.doiTuong;
                var _productname = product.tenSP;
                var _quality = product.chiTietSanPhams[i].soLuong;
                var _price = product.chiTietSanPhams[i].giaSP;

                str += '<td class="columnProductDetailSTT tableProductDetailPadding">' + (i+1) + '</td>';
                str += '<td class="columnProductDetailStick tableProductDetailPadding"><input onclick="highlight_row(this)" id="' + product.chiTietSanPhams[i].maCTSP + '" type="checkbox" value="' + product.chiTietSanPhams[i].maCTSP + '"/></td>';
                str += '<td class="columnProductDetail_ProductCode tableProductDetailPadding word_break" title="' + product.maSP + '">' + product.maSP + '</td>';
                str += '<td class="columnProductDetailCode tableProductDetailPadding word_break" title="' + product.chiTietSanPhams[i].maCTSP + '">' + product.chiTietSanPhams[i].maCTSP + '</td>';
                if(_catelogy) {
                    str += '<td class="columnProductDetail_Category tableProductDetailPadding" id="' + product.loaiDanhMucSanPham.maLoaiDMSP + '">' + product.loaiDanhMucSanPham.tenLoaiDMSP + '</td>';
                } else {
                    str += '<td class="columnProductDetail_Category tableProductDetailPadding">Trống</td>';
                }
                if(_productname) {
                    str += '<td class="columnProductDetailTitle tableProductDetailPadding" title="' + product.tenSP + '">' + product.tenSP.trunc(100,true) + '</td>';
                } else {
                    str += '<td class="columnProductDetailTitle tableProductDetailPadding">Trống</td>';
                }

                if(_color) {
                    str += '<td class="columnProductDetailColor tableProductDetailPadding" id="' + product.chiTietSanPhams[i].mauSac.maMau + '">' + product.chiTietSanPhams[i].mauSac.tenMau + '</td>';
                } else {
                    str += '<td class="columnProductDetailColor tableProductDetailPadding">Trống</td>';
                }

                if(_size) {
                    str += '<td class="columnProductDetailSize tableProductDetailPadding" id="' + product.chiTietSanPhams[i].kichThuoc.maSize + '">' + product.chiTietSanPhams[i].kichThuoc.tenSize + '</td>';
                } else {
                    str += '<td class="columnProductDetailSize tableProductDetailPadding">Trống</td>';
                }

                if(_sexual) {
                    str += '<td class="columnProductDetailSexual tableProductDetailPadding">' + product.doiTuong + '</td>';
                } else {
                    str += '<td class="columnProductDetailSexual tableProductDetailPadding">Trống</td>';
                }

                if(_images[0]) {
                    str += '<td class="columnProductDetailPicture tableProductDetailPadding"><img src="'+ ISS.SESSION.getSession('contextPath') + _images[0].tenHinh + '" width="100px"></td>';
                } else {
                    str += '<td class="columnProductDetailPicture tableProductDetailPadding">Trống</td>';
                }

                if(_motif) {
                    str += '<td class="columnProductDetailPatern tableProductDetailPadding" id="' + product.chiTietSanPhams[i].hoaTiet.maHoaTiet + '">' +  product.chiTietSanPhams[i].hoaTiet.tenHoaTiet + '</td>';
                } else {
                    str += '<td class="columnProductDetailPatern tableProductDetailPadding">Trống</td>';
                }

                if(_quality) {
                    str += '<td class="columnProductDetailQuantity tableProductDetailPadding">' +   product.chiTietSanPhams[i].soLuong + '</td>';
                } else {
                    str += '<td class="columnProductDetailQuantity tableProductDetailPadding">Trống</td>';
                }

                if(_price) {
                    str += '<td class="columnProductDetailPrice tableProductDetailPadding">'+ formatCurency(product.chiTietSanPhams[i].giaSP) +'</td>';
                } else {
                    str += '<td class="columnProductDetailPrice tableProductDetailPadding">Trống</td>';
                }
            }

            str += '</tr>';
        }
    }
    str +=   '</tbody>';
    str += '</table>';

    ISS.DOM.html('#ProductDetail_All', str);

}
